#include "testDrive.h"

string g_msg;

void mathTestRun( string str, big_number *rez1, big_number *rez2, big_number *expect, int line){
	bool pass = (((*rez1) == (*expect))&&((*rez2) == (*expect)));
	ostringstream oss;
	oss << str << ": result1: " <<  *rez1 << ": result1: " <<  *rez2 << " expected: "  << *expect << " pass: " << boolalpha << pass;
	g_msg = oss.str();
	if (pass)
		logger::success(oss.str(), "mathTest.txt", line);
	else
		logger::fail(oss.str(), "mathTest.txt", line);
}

void mathTest(big_number *num1, big_number *num2, char ch, big_number *num3, int line)
{
	big_number tmp1, tmp2;
	tmp1 = (*num1);
	switch (ch)
	{
	case '+': 
		tmp1 += (*num2);
		tmp2 = (*num1)+(*num2);
		break;
	case '-': 
		tmp1 -= (*num2);
		tmp2 = (*num1)-(*num2);
		break;
	case '*': 
		tmp1 *= (*num2);
		tmp2 = (*num1)*(*num2);
		break;
	case '/': 
		tmp1 /= (*num2);
		tmp2 = (*num1)/(*num2);
		break;
	case '%': 
		tmp1 = (*num1).toBigInteger() % (*num2).toBigInteger();
		tmp2 = tmp1;
		break;
	case 's': 
		tmp1 = (*num1).SQRT();
		tmp2 = tmp1;
		break;
	default:
		break;
	}
	if ( ch == 's')
		mathTestRun( (*num1).toString() + " SQRT", &tmp2, &tmp1, num3, line);
	else
		mathTestRun( (*num1).toString() + " "+ ch + " " + (*num2).toString(), &tmp2, &tmp1, num3, line);
}


void mathParser(string *arr, big_number **a, big_number **b, big_number **c, char *ch, bool *fatch){
	if ( arr[0].compare("i") == 0)
		*a = new big_integer(arr[1]);
	else
		*a=  new big_number(arr[1]);

	if ( arr[2].compare("i") == 0)
		*b = new big_integer(arr[3]);
	else
		*b =  new big_number(arr[3]);

	*ch = *(arr[4].c_str());

	*c = new big_number(arr[5]);

	*fatch = arr[6].compare("1") == 0;
}

void mathOperationTest(){
	PNode tmp, ptr = readFile("mathTest.txt");
	string arr[7];
	big_number *a, *b, *c;
	char ch;
	bool fatch = false;
	while ( ptr != NULL ){
		try { 
			splitByComma(arr, ptr->line);
			mathParser(arr, &a, &b, &c, &ch, &fatch);
			mathTest(a, b, ch, c, ptr->lineNumber);
		} 
		catch( const std::exception &e) {
			cout << e.what() << endl;
			if ( fatch )
			{
				logger::success(g_msg, "mathTest.txt", ptr->lineNumber);
			}
			else
			{
				logger::exception(ptr->line, "mathTest.txt", ptr->lineNumber);
			}
		}
		tmp = ptr;
		ptr = ptr->next;
		freeNode(tmp);
	}
}